﻿/*******************************************************************************
 * Copyright © 2016 NFine.Framework 版权所有
 * Author: NFine
 * Description: NFine快速开发平台
 * Website：http://www.nfine.cn
*********************************************************************************/
namespace Zero.Code
{
    public class OperatorProvider
    {
        public static OperatorProvider Provider
        {
            get { return new OperatorProvider(); }
        }
        private string LoginUserKey = "nfine_loginuserkey_2016";
        //private string LoginProvider = Configs.GetValue("LoginProvider");

        /// <summary>
        /// 获取当前操作对象不解密
        /// </summary>
        /// <returns></returns>
        public OperatorModel GetCurrentNoEncrypt()
        {
            OperatorModel operatorModel = new OperatorModel();
            operatorModel = WebHelper.GetCookie(LoginUserKey).ToObject<OperatorModel>();
            //operatorModel.UserId = model["L_UserId"].ToString();
            return operatorModel;
        }
        /// <summary>
        /// 获取当前操作对象需解密
        /// </summary>
        /// <returns></returns>

        public OperatorModel GetCurrent()
        {
            OperatorModel operatorModel = new OperatorModel();
            //if (LoginProvider == "Cookie")
            //{
            //    operatorModel = DESEncrypt.Decrypt(WebHelper.GetCookie(LoginUserKey).ToString()).ToObject<OperatorModel>();
            //}
            //else
            //{
            //    operatorModel = DESEncrypt.Decrypt(WebHelper.GetSession(LoginUserKey).ToString()).ToObject<OperatorModel>();
            //}
            return operatorModel;
        }
        /// <summary>
        /// 添加当前操作对象
        /// </summary>
        /// <param name="operatorModel"></param>
        public void AddCurrentOperator(OperatorModel operatorModel)
        {
            WebHelper.WriteCookie("nfine_loginuserkey_2016", operatorModel.ToJson());//将当前登录用户信息写入cookie
        }
        public void AddCurrent(OperatorModel operatorModel)
        {
            //if (LoginProvider == "Cookie")
            //{
            //    WebHelper.WriteCookie(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()), 60);
            //}
            //else
            //{
            //    WebHelper.WriteSession(LoginUserKey, DESEncrypt.Encrypt(operatorModel.ToJson()));
            //}
            WebHelper.WriteCookie("nfine_mac", Md5.md5(Net.GetMacByNetworkInterface().ToJson(), 32));
            WebHelper.WriteCookie("nfine_licence", Licence.GetLicence());
        }
        public void RemoveCurrent()
        {
            //if (LoginProvider == "Cookie")
            //{
            //    WebHelper.RemoveCookie(LoginUserKey.Trim());
            //}
            //else
            //{
            //    WebHelper.RemoveSession(LoginUserKey.Trim());
            //}
        }
    }
}
